/**
 *	@file	np_main.c
 *	@brief	アンノーンノートアプリメイン
 *	@author	Miyuki Iwasawa
 *	@date	08.08.04
 *
 */

#include "common.h"
#include "system/procsys.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/snd_tool.h"
#include "system/fontproc.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/buflen.h"
#include "system/window.h"
#include "system/brightness.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/palanm.h"
#include "system/wipe.h"
#include "system/window.h"
#include "system/font_arc.h"
#include "system/winframe.naix"
#include "gflib/touchpanel.h"
#include "itemtool/myitem.h"
#include "field/syswork.h"
#include "msgdata/msg.naix"

#include "savedata/config.h"
#include "application/an_note.h"
#include "an_note_sys.h"
#include "an_note_sub.h"
#include "an_note_snd_def.h"

#if 0
#ifdef DEBUG_ONLY_FOR_iwasawa
 #undef IWASAWA_PRINT
 #define IWASAWA_PRINT(...) ((void) 0)
#endif
#endif

//=========================================================
//プロトタイプ定義
//=========================================================
static void sub_ViewClear(void);
static void app_InitData(AN_NOTE_WORK* wk);
static void app_EndData(AN_NOTE_WORK* wk);
static BOOL app_Init(AN_NOTE_WORK* wk);
static BOOL app_End(AN_NOTE_WORK* wk);

static int seq_FadeIn(AN_NOTE_WORK* wk);
static int seq_FadeOut(AN_NOTE_WORK* wk);
static int seq_Main(AN_NOTE_WORK* wk);
static int seq_PageChange(AN_NOTE_WORK* wk);
static int seq_MsgWait(AN_NOTE_WORK* wk);

static BOOL sub_MsgWait(AN_NOTE_WORK* wk);

//=========================================================
//プロセス定義
//=========================================================
/**
 *	@brief	アンノーンノートアプリ呼び出し
 */
PROC_RESULT AnNoteProc_Init(PROC* proc,int* seq)
{

	switch(*seq){
	case 0:
		sub_ViewClear();

		//ヒープ作成
		HeapStatePush();
		sys_CreateHeap(HEAPID_BASE_APP,HEAPID_AN_NOTE,0x20000);

		{
			AN_NOTE_WORK* wk = NULL;
			wk = PROC_AllocWork(proc,sizeof(AN_NOTE_WORK),HEAPID_AN_NOTE);
			MI_CpuClear8(wk,sizeof(AN_NOTE_WORK));

			wk->heapID = HEAPID_AN_NOTE;
			wk->param = (AN_NOTE_PARAM*)PROC_GetParentWork(proc);

			//引継ぎデータ初期化
			app_InitData(wk);

		}
		(*seq)++;
		break;
	case 1:
		if(app_Init((AN_NOTE_WORK*)PROC_GetWork(proc))){
			return PROC_RES_FINISH;
		}
	}
	return PROC_RES_CONTINUE;
}

/**
 *	@brief	アンノーンノートモジュールメイン
 */
PROC_RESULT AnNoteProc_Main(PROC* proc,int* seq)
{
	AN_NOTE_WORK* wk = (AN_NOTE_WORK*)PROC_GetWork(proc);

	switch(*seq){
	case SEQ_FADE_IN:
		*seq = seq_FadeIn(wk);
		break;
	case SEQ_FADE_OUT:
		*seq = seq_FadeOut(wk);
		break;
	case SEQ_MAIN:
		*seq = seq_Main(wk);
		break;
	case SEQ_PAGE_CHG:
		*seq = seq_PageChange(wk);
		break;
	case SEQ_EXIT:
		return PROC_RES_FINISH;
	}
	CATS_Draw(wk->pActRes);
	CATS_Draw(wk->pActIconRes);
	return PROC_RES_CONTINUE;
}

/**
 *	@brief	アンノーンノートモジュール終了
 */
PROC_RESULT AnNoteProc_End(PROC* proc,int* seq)
{
	AN_NOTE_WORK* wk = (AN_NOTE_WORK*)PROC_GetWork(proc);

	if(!app_End(wk)){
		return PROC_RES_CONTINUE;
	}
	sub_ViewClear();

	//引継ぎデータ書き戻し
	app_EndData(wk);

	//ワーク解放
	PROC_FreeWork(proc);

	HeapStatePop();
	HeapStateCheck(HEAPID_AN_NOTE);

	sys_DeleteHeap(HEAPID_AN_NOTE);

	return PROC_RES_FINISH;
}

////////////////////////////////////////////////////////////////////////
//サブ

/**
 *	@brief	画面初期化
 */
static void sub_ViewClear(void)
{
	sys_VBlankFuncChange(NULL, NULL);
	sys_HBlankIntrStop();	//HBlank割り込み停止

	GF_Disp_GX_VisibleControlInit();
	GF_Disp_GXS_VisibleControlInit();
	GX_SetVisiblePlane(0);
	GXS_SetVisiblePlane(0);

	WIPE_SetBrightness(WIPE_DISP_MAIN,WIPE_FADE_BLACK);
	WIPE_SetBrightness(WIPE_DISP_SUB,WIPE_FADE_BLACK);
	WIPE_ResetWndMask(WIPE_DISP_MAIN);
	WIPE_ResetWndMask(WIPE_DISP_SUB);
}

#if GS_AFTERMASTER_GF1047_20090929_FIX
/*
 *  @brief  アンノーンのフォルム"!"と"?"、どちらを先に捕まえた(図鑑に登録した)か？
 *
 *  @return 2種の図鑑登録数
 */
static int sub_FormEntryNoCheckEQ(const ZUKAN_WORK* zukan,u8 buf[2])
{
  int i,num = 0;
  u8 form;

  for(i = 0;i < UNK_END;i++){
		form = ZukanWork_GetPokeAnnoonForm(zukan, i, TRUE);
    if(form == UNK_ENC || form == UNK_QUE){
      buf[num++] = form;
    }
  }
  return num;
}

#endif

/**
 *	@brief	引継ぎデータ初期化
 */
static void app_InitData(AN_NOTE_WORK* wk)
{
	int i;
	CONFIG* config;

	//キーモード初期化
	wk->key_mode = KeyTouchStatus_CheckTouchOrKey(wk->param->kt_status);

	//コンフィグ
	config = SaveData_GetConfig(wk->param->save);
	wk->msg_spd = CONFIG_GetMsgPrintSpeed(config);
	wk->win_type = CONFIG_GetWindowType(config);

	wk->zukan_sv = SaveData_GetZukanWork(wk->param->save);

	//メモページmax数
	wk->an_memo_open = SysWork_AnnoonNoteMemoOpenLevelGet( SaveData_GetEventWork(wk->param->save));
	wk->memo_page_max = AnNoteSub_GetMemoPageMax(wk);

	//アンノーン捕獲済み種類数
	wk->an_get_num = ZukanWork_GetPokeAnnoonNum(wk->zukan_sv,TRUE);

//	wk->an_get_num = AN_FORM_MAX;
	if(wk->an_memo_open >= AN_MEMO_GET_ATOZ){
#if GS_AFTERMASTER_GF1047_20090929_FIX
		for(i = 0;i <= UNK_Z;i++){  //A-Zまでは順番どおり
			wk->an_get_form[i] = i;
		}
    { //!と?は図鑑登録順に並ぶ
      u8 num,form[2];
      num = sub_FormEntryNoCheckEQ(wk->zukan_sv,form);
      for(i = 0;i < num;i++){
        wk->an_get_form[i+UNK_ENC] = form[i];
      }
    }
#else
		for(i = 0;i < wk->an_get_num;i++){
			wk->an_get_form[i] = i;
		}
#endif
	}else{
		for(i = 0;i < wk->an_get_num;i++){
			wk->an_get_form[i] =
				ZukanWork_GetPokeAnnoonForm(wk->zukan_sv, i, TRUE);
		}
	}
	wk->an_page_max = wk->an_get_num/(ICON_LINEMAX*2);
	if((wk->an_get_num > 0) && wk->an_get_num % (ICON_LINEMAX*2)){
		wk->an_page_max++;
	}
	//ページmax
	wk->page_max = wk->an_page_max+wk->memo_page_max+1;
}

/**
 *	@brief	引継ぎデータ終了処理
 */
static void app_EndData(AN_NOTE_WORK* wk)
{
	//キーモード書き戻し
	KeyTouchStatus_SetTouchOrKey(wk->param->kt_status,wk->key_mode);
}

/**
 *	@brief	初期化
 *
 */
static BOOL app_Init(AN_NOTE_WORK* wk)
{
	switch(wk->seq){
	case 0:
		AnNoteResInit(wk);
		++wk->seq;
		break;
	case 1:
		AnNoteInitialDraw(wk);

		//タッチパネル初期化
		InitTPSystem();
		InitTPNoBuff(1);

		Snd_DataSetByScene( SND_SCENE_SUB_PLANTER, 0, 0 );
		wk->seq = 0;
		return TRUE;
	}
	return FALSE;

}

/**
 *	@brief	終了処理
 *
 */
static BOOL app_End(AN_NOTE_WORK* wk)
{
	//タッチパネル終了
	StopTP();

	AnNoteResRelease(wk);
	return TRUE;
}

/**
 *	@brief	フェードイン
 *
 */
static int seq_FadeIn(AN_NOTE_WORK* wk)
{
	switch(wk->seq){
	case 0:
		WIPE_SYS_Start(
			WIPE_PATTERN_WMS, WIPE_TYPE_FADEIN,
			WIPE_TYPE_FADEIN, WIPE_FADE_BLACK, WIPE_DEF_DIV, WIPE_DEF_SYNC, wk->heapID);
		wk->seq++;
		break;
	case 1:
		if( !WIPE_SYS_EndCheck()){
			break;
		}
		wk->seq = 0;
		return SEQ_MAIN;
	}
	return SEQ_FADE_IN;
}

/**
 *	@brief	フェードアウト
 *
 */
static int seq_FadeOut(AN_NOTE_WORK* wk)
{
	switch(wk->seq){
	case 0:
		if(AnNoteSub_BackButtonIsAnime(wk)){
			break;
		}
		WIPE_SYS_Start(
			WIPE_PATTERN_WMS, WIPE_TYPE_FADEOUT,
			WIPE_TYPE_FADEOUT, WIPE_FADE_BLACK, WIPE_DEF_DIV, WIPE_DEF_SYNC, wk->heapID);
		wk->seq++;
		break;
	case 1:
		if( !WIPE_SYS_EndCheck()){
			break;
		}
		wk->seq = 0;
		return SEQ_EXIT;
	}
	return SEQ_FADE_OUT;
}

/**
 *	@brief	メイン
 */
static int seq_Main(AN_NOTE_WORK* wk)
{
	return AnNoteSub_GetInput(wk);
}

/**
 *	@brief	ページ変更
 */
static int seq_PageChange(AN_NOTE_WORK* wk)
{
	switch(wk->seq){
	case 0:
		WIPE_SYS_Start(
			WIPE_PATTERN_WMS, WIPE_TYPE_SHUTTEROUT_UP,
			WIPE_TYPE_SHUTTEROUT_DOWN, WIPE_FADE_BLACK, WIPE_DEF_DIV, WIPE_DEF_SYNC, wk->heapID);
		wk->seq++;
		break;
	case 1:
		if( !WIPE_SYS_EndCheck()){
			break;
		}
		AnNoteSub_DrawPage(wk,wk->page);
		wk->seq++;
		break;
	case 2:
		WIPE_SYS_Start(
			WIPE_PATTERN_WMS, WIPE_TYPE_SHUTTERIN_DOWN,
			WIPE_TYPE_SHUTTERIN_UP, WIPE_FADE_BLACK, WIPE_DEF_DIV, WIPE_DEF_SYNC, wk->heapID);
		wk->seq++;
		break;
	case 3:
		if( !WIPE_SYS_EndCheck()){
			break;
		}
		wk->seq = 0;
		return SEQ_MAIN;
	}
	return SEQ_PAGE_CHG;
}
